﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PTExpress.Templates
{
    /// <summary>
    /// Interaction logic for MaskTextBox.xaml
    /// </summary>
    public partial class MaskTextBox : UserControl
    {
        public MaskTextBox()
        {
            InitializeComponent();
            this.Background = System.Windows.Media.Brushes.White;
        }

        string MaskText = "0123456789";

        public string InputMask
        {
            get { return (string)GetValue(InputMaskProperty); }
            set { SetValue(InputMaskProperty, value); }
        }

        // Using a DependencyProperty as the backing store for InputMask.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InputMaskProperty =
            DependencyProperty.Register("InputMask", typeof(string), typeof(MaskTextBox), new UIPropertyMetadata(null));



        public object Value
        {
            get { return (object)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(MaskTextBox), new UIPropertyMetadata(null));



        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(MaskTextBox), new UIPropertyMetadata("0"));


        //public System.Windows.FlowDirection FlowDirection
        //{
        //    get { return (System.Windows.FlowDirection)GetValue(FlowDirectionProperty); }
        //    set { SetValue(FlowDirectionProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for FlowDirection.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty FlowDirectionProperty =
        //    DependencyProperty.Register("FlowDirection", typeof(System.Windows.FlowDirection), typeof(MaskTextBox), new UIPropertyMetadata(System.Windows.FlowDirection.RightToLeft));



        public int MinValue
        {
            get { return (int)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MinValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(int), typeof(MaskTextBox), new UIPropertyMetadata(0));

        public int MaxValue
        {
            get { return (int)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MaxValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(int), typeof(MaskTextBox), new UIPropertyMetadata(0));

        private bool CheckInputMask(string text)
        {
            if (InputMask == null)
                return true;
            if (string.IsNullOrEmpty(text))
                return false;
            string[] s = InputMask.Split('-', ',');
            int valueInput = int.Parse(text);
            int value1 = int.Parse(s[0]);
            int value2 = int.Parse(s[1]);
            if (valueInput >= value1 && valueInput <= value2)
                return true;
            return false;
        }

        private bool CheckMask(string text)
        {
            if (InputMask == null)
                return true;
            if (string.IsNullOrEmpty(text))
                return false;
            string[] s = InputMask.Split('-', ',');
            int valueInput = int.Parse(text);
            int value1 = int.Parse(s[0]);
            int value2 = int.Parse(s[1]);
            if (valueInput > value2)
                return true;
            return false;
        }
        private void mtxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //e.Handled = true;

            mtxt.Background = this.Background;
            if (!MaskText.Contains(e.Text))
            {
                e.Handled = true;
                return;
            }
            if (CheckMask(mtxt.Text + e.Text))
                //{
                //    if (CheckInputMask(e.Text) || int.Parse(e.Text) < MinValue)
                //        mtxt.Text = null;
                //    else
                //        e.Handled = true;
                //}
                //if (mtxt.Text == "0")
                mtxt.Text = null;
            //mtxt.FlowDirection = System.Windows.FlowDirection
        }

        private void mtxt_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (!CheckInputMask(mtxt.Text))
            {
                e.Handled = true;
                mtxt.Background = System.Windows.Media.Brushes.Red;
            }
        }

        private void mtxt_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                mtxt.CaretIndex = mtxt.Text.Length;
                mtxt.SelectAll();
            }
            catch { }
        }

        private void mtxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Value = mtxt.Text;
            if (InputMask != null && mtxt.Text != null)
            {
                string[] s = InputMask.Split(',', '-');
                if (mtxt.Text.Length == s[1].Length)
                    mtxt.SelectAll();
            }
        }

        private void UcMaskTextBox_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                string[] s = InputMask.Split(',', '-');
                MinValue = int.Parse(s[0]);
                MaxValue = int.Parse(s[1]);
            }
            catch { }
        }

        private void UcMaskTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            mtxt.Focus();
        }

        public void GetFocus()
        {
            mtxt.Focus();
        }
    }
}
